clc;
clear all;
 
% principle office 2nd 
 
%  x=[0;97;0;81.5];
%  y=[-256.0;-256.0;-177.5;-177.5];

%mapping  
x=[0;97;0;81.5];
y=[-78.5;-78.5;0;0];

%2nd mapping
%x=[0;97;0;81.5];
y%=[-256.0;-256.0;-177.5;-177.5];

orignal=imread('facultySecond.jpg');
  
 figure, imshow(orignal);
 
 % disp 'select points required and press enter';
 [xd yd]=ginput(4);
 xd
 yd
 save('myfile.mat', 'orignal', 'x', 'y', 'xd', 'yd');
 
load('myfile.mat');

m = zeros(8, 9);
for i=1:4
    m(2*i-1,:)= [x(i),y(i),1,0,0,0, -x(i)*xd(i),-xd(i)*y(i),-xd(i)];
    m(2*i, :)= [0,0,0,x(i),y(i),1, -x(i)*yd(i),-y(i)*yd(i),-yd(i)];
end;

H1 = null(m);
H = H1/H1(9);
H = reshape(H1,3,3);

% tform = maketform('projective',inv(H));
% resimg = imtransform(orignal, tform);
% 
% subplot(2,1,1), imshow(orignal,[]);
% subplot(2,1,2), imshow(resimg,[]);
 
% map to real word coordinates



figure, imshow(orignal);

[xp yp]=ginput(1);
xp
yp

r = [xp; yp; 1];
r
% 
% Hinv = inv(H);
% Hinv
% 
% rp = Hinv * r;
% rp





r = [x(1); y(1); 1]
rd = H * r;
rd = rd /rd(3)
rc = inv(H) * rd;
rc = rc / rc(3)


r = [x(2); y(2); 1]
rd = H * r;
%rd = rd /rd(3)
rc = inv(H) * rd

r = [x(3); y(3); 1]
rd = H * r;
%rd = rd /rd(3)
rc = inv(H) * rd

r = [x(4); y(4); 1]
rd = H * r;
%rd = rd /rd(3)
rc = inv(H) * rd
